import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';
import '../../../controller/match/detail/match_odds_logic.dart';
import '../../../entity/match/zq_odds_entity.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/ads_label.dart';
import '../../../widgets/choice_bar.dart';

class LqOddsView extends StatefulWidget {
  const LqOddsView({Key? key}) : super(key: key);

  @override
  State<LqOddsView> createState() => _LqOddsViewState();
}

class _LqOddsViewState extends State<LqOddsView> {
  final logic = Get.put(LqOddsLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LqOddsLogic>(builder: (logic) {
      return Column(
        children: [
          Container(
              color: Colours.white,
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 5),
              child: ChoiceBar(
                  items: logic.typeList,
                  backgroundColor: Colours.redFEECEC,
                  textColor: Colours.main,
                  onTap: (index) {
                    Utils.onEvent("zqbsxq_zs", params: {"zqbsxq_zs": index});
                    logic.typeIndex = index;
                  })),
          Expanded(
            child: EasyRefresh.builder(
              onRefresh: () async {
                await logic.requestData();
              },
              header: Styles.refreshHeader(position: IndicatorPosition.locator),
              childBuilder: (context, physics) {
                return TabBarView(
                  physics: const NeverScrollableScrollPhysics(),
                  controller: logic.tabController,
                  children: List.generate(
                    logic.typeList.length, (index) => CustomScrollView(
                      physics: physics,
                      slivers: [
                        const HeaderLocator.sliver(),
                        SliverToBoxAdapter(
                          child: AdsLabel(
                            adsTag: "match_lq_指数_$index",
                            tapType: 'ggw_bsxq_zs',
                            margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
                          ),
                        ),
                        LoadingWidget<int>(
                          isLoading: false,
                          data: logic.data.length,
                          noData: NoDataWidget.sliver(
                              tip: "暂无${logic.typeList[index]}数据"
                          ),
                          child: () => SliverToBoxAdapter(
                            child: Styles.card(
                              color: Colours.white,
                              margin: const EdgeInsets.only(
                                  top: 8, bottom: 30, left: 8, right: 8),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                        flex: 2,
                                        child: companyList(index)
                                      ),
                                      Expanded(
                                        flex: 8,
                                        child: valueList(index)
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ]
                    ),
                  )
                );
              },
            ),
          )
        ],
      );
    });
  }

  Widget companyList(int index){
    return Column(
      mainAxisAlignment:
      MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
        (logic.data[index]?.length ?? 0) + 1,
        (i) => Container(
          height: i == 0 ? 32 : 60,
          alignment: Alignment.centerLeft,
          margin: const EdgeInsets.only(left: 12),
          decoration: const BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 0.5, color: Colours.greyEE))
          ),
          child: QkdText(
            i == 0 ? "公司"
              : logic.data[index]?[i - 1].companyName ?? "",
            fontSize: 11
          )
        )
        .tap(() {
          if (i != 0) {
            Utils.onEvent("lqbsxq_zs",
                params: {"lqbsxq_zs": 4});
            Get.toNamed(
              Routes.lqOddsDetail,
              arguments: [
                logic.data[index]?[i - 1].companyId,
                logic.detail.matchId,
                index
              ]
            );
          }
        })
      ),
    );
  }

  Widget valueList(int index){
    return SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Column(
          crossAxisAlignment:
          CrossAxisAlignment.start,
          children: List.generate(
              (logic.data[index]?.length ?? 0) + 1,
                  (i) => i == 0
                  ? header(index)
                  : item(logic.data[index]?[i - 1], index)
          ),
        )
    ).paddingOnly(right: 16);
  }

  Widget item(ZqOddsEntity? entity, int index) {
    var firstData = logic.formSingleLine(entity?.firstData ?? []);
    var lastData = logic.formSingleLine(entity?.lastData ?? []);
    return GestureDetector(
      onTap: () => Get.toNamed(Routes.lqOddsDetail, arguments: [
        entity?.companyId,
        logic.detail.matchId,
        index
      ]),
      child: Container(
        height: 60,
        padding: const EdgeInsets.symmetric(vertical: 10),
        decoration: const BoxDecoration(
            border:
                Border(bottom: BorderSide(width: 0.5, color: Colours.greyEE))),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              children: [
                Container(
                  width: 30,
                  alignment: Alignment.center,
                  child: QkdText("初",
                    fontSize: 11,color: Colours.grey66
                  )
                ),
                Container(
                    width: index == 0 ? 68 : 52,
                    alignment: Alignment.center,
                    child: firstData[0]),
                if (index != 0)
                  Container(
                    width: 40,
                    alignment: Alignment.center,
                    child: QkdText(entity?.line ?? "", fontSize: 12).center,
                  ),
                Container(
                    width: index == 0 ? 68 : 52,
                    alignment: Alignment.center,
                    child: firstData[2]),
                Container(
                  width: 68,
                  alignment: Alignment.center,
                  child: firstData[3]),
                Container(
                  width: 60,
                  alignment: Alignment.center,
                  child: QkdText(
                    entity?.firstRate.noValue == true ? ""
                      : "${(double.parse(entity!.firstRate!) * 100).toStringAsFixed(2)}%",
                    fontSize: 12
                  ),
                )
              ],
            ),
            Row(
              children: [
                Container(
                    width: 30,
                    alignment: Alignment.center,
                    child: QkdText("即",
                      fontSize: 11, color: Colours.grey99
                    )
                ),
                Container(
                    width: index == 0 ? 68 : 52,
                    alignment: Alignment.center,
                    child: lastData[0]),
                if (index != 0)
                  Container(
                    width: 40,
                    alignment: Alignment.center,
                    child: QkdText(entity?.line ?? "",
                      fontSize: 12
                    ),
                  ),
                Container(
                    width: index == 0 ? 68 : 52,
                    alignment: Alignment.center,
                    child: lastData[2]),
                Container(
                    width: 68, alignment: Alignment.center, child: lastData[3]),
                Container(
                  width: 60,
                  alignment: Alignment.center,
                  child: QkdText(
                    entity?.lastRate.noValue == true
                        ? ""
                        : "${(double.parse(entity!.lastRate!) * 100).toStringAsFixed(2)}%",
                    fontSize: 12
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget header(int index) {
    return Container(
      height: 32,
      decoration: const BoxDecoration(
          border:
              Border(bottom: BorderSide(width: 0.5, color: Colours.greyEE))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
              width: 30,
              alignment: Alignment.center,
              child: QkdText("即",
                fontSize: 11, color: Colours.transparent
              )
          ),
          Container(
              width: index == 0 ? 68 : 52,
              alignment: Alignment.center,
              child: QkdText(logic.headTypeList[index][0],
                fontSize: 12
              )
          ),
          Container(
            width: index == 0 ? 68 : 40,
            alignment: Alignment.center,
            child: QkdText(logic.headTypeList[index][1],
              fontSize: 12
            ),
          ),
          if (index != 0)
            Container(
                width: 52,
                alignment: Alignment.center,
                child: QkdText(logic.headTypeList[index][2],
                  fontSize: 12
                )
            ),
          Container(
              width: 68,
              alignment: Alignment.center,
              child: QkdText("凯利",
                fontSize: 12
              )
          ),
          Container(
            width: 60,
            alignment: Alignment.center,
            child: QkdText("返还率",
              fontSize: 12
            )
          )
        ],
      ),
    );
  }
}
